#include "GeneratingState.h"
#include "InputManager.h"
#include "EffectManager.h"
#include "Game.h"
#include "Building.h"
#include "MainState.h"

GeneratingState GeneratingState::m_GeneratingState;

void GeneratingState::Init()
{
	m_GenObject = new Building;
	m_GenObject->Generate(D3DXVECTOR3(0,0,0),D3DXVECTOR3(10,60,10));

	m_pCamera = new Camera;
	m_pCamera->Load("Static");

	////Default Light
	m_vLightDir.x = 10.0;
	m_vLightDir.y = -10.0;
	m_vLightDir.z = 0.0;
	D3DLIGHT9 light;
	ZeroMemory( &light, sizeof(D3DLIGHT9) );
	light.Type       = D3DLIGHT_DIRECTIONAL;
	light.Diffuse.r  =0.8f;
	light.Diffuse.g  = 0.8f;
	light.Diffuse.b  = 0.8f;
	light.Diffuse.a  = 1.0f;
	light.Range      = 10.0f;
	D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &m_vLightDir );

	// Tell the device about the light and turn it on
	TheD3DApp::Instance()->GetD3DDev()->SetLight( 0, &light );
	TheD3DApp::Instance()->GetD3DDev()->LightEnable( 0, TRUE ); 
	TheD3DApp::Instance()->GetD3DDev()->SetRenderState( D3DRS_LIGHTING, TRUE );
	TheD3DApp::Instance()->GetD3DDev()->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_XRGB(50,50,50) );

	// Fill in a light structure defining our light
	ZeroMemory( &light, sizeof(D3DLIGHT9) );
	light.Type       = D3DLIGHT_POINT;
	light.Diffuse.r  = 0.5f;
	light.Diffuse.g  = 0.5f;
	light.Diffuse.b  = 0.5f;

	// Point lights have no direction but do have a position
	light.Position = D3DXVECTOR3(100,20,100);

	// Tell the device about the light and turn it on
	light.Attenuation0 = 0.1f;
	light.Range=200.0f;
	TheD3DApp::Instance()->GetD3DDev()->SetLight( 2, &light );
	TheD3DApp::Instance()->GetD3DDev()->LightEnable( 2, TRUE );
}

void GeneratingState::Clean()
{
	m_GenObject->Clean();
	m_GenObject=NULL;
}

void GeneratingState::Pause(){}
void GeneratingState::Resume(){}
void GeneratingState::HandleCollisions(){}

void GeneratingState::HandleInput()
{
	if(TheInputManager::Instance()->KeyPressed(DIK_P) && !m_bWireframe)
	{
		TheEffectManager::Instance()->SetWireframe(!TheEffectManager::Instance()->GetWireframe());
		m_bWireframe=true;
	}
	else if (!TheInputManager::Instance()->KeyPressed(DIK_P))
	{
		m_bWireframe=false;
	}
	if(TheInputManager::Instance()->KeyPressed(DIK_M))
	{
		TheGame::Instance()->ChangeState(MainState::Instance());
	}
	if(TheInputManager::Instance()->KeyPressed(DIK_V))
	{
		m_GenObject = new Building;
		//m_GenObject->Generate(D3DXVECTOR3(0,0,0),D3DXVECTOR3(10,60,10));
		m_GenObject->Generate(D3DXVECTOR3(0,0,0),Triangle(D3DXVECTOR3(0,0,0),D3DXVECTOR3(0,0,10),D3DXVECTOR3(10,0,10)),30);
		D3DXVECTOR3 d[4];
		//m_GenObject->Generate(D3DXVECTOR3(0,0,0),d,60);

	}
	if(TheInputManager::Instance()->KeyPressed(DIK_B) && !pressed)
	{
		pressed=true;
		m_GenObject->NextStep();
	}
	if(!TheInputManager::Instance()->KeyPressed(DIK_B))
	{
		pressed=false;
	}
}

void GeneratingState::Update(float dt)
{
	TheInputManager::Instance()->Update();
	m_pCamera->Update();
}
void GeneratingState::Render()
{
	D3DXMATRIX world;
	D3DXMatrixIdentity(&world);

	world *= (m_pCamera->GetViewMatrix() * m_pCamera->GetProjectionMatrix());
	std::vector<GameObject*> go;
	go.push_back(m_GenObject);
	TheEffectManager::Instance()->Render(m_EffectName, m_pCamera, go);

	TheD3DApp::Instance()->GetD3DDev()->EndScene();

	TheD3DApp::Instance()->GetD3DDev()->Present(NULL, NULL, NULL, NULL);

}